﻿namespace ClientWF
{   using System.ComponentModel;
    using System.Reflection;
    using System;
    using System.Windows.Forms;
    using System.Text;
    using System.Drawing;

    public partial class LoginRoom : Form, IView
    {

        public IPresenter _presenter { get; set; }
        public IGame _game { get; set; }    
        string InvitationStatus;
        string OpNickName; 
        bool SelectFlag=false;   

        public LoginRoom()
        {
            InitializeComponent();
            UnloggedView();
        }

        public void show(string information, string command)
        {
            LoggedView();
            switch (command)
            {
                case "Declined": InvitationStatus = information; break;
                case "ok":             
                    _game.updateGameStatus(information); break;
                case "Invited": startGame(information);
                    break;
                case "Endgame": _game.EndGame(information); break;
                default: UserListUpdate(information);
                    break;
            }
        }
        public void startGame(string inf)
        {
            string[] split = inf.Split('^');
            string message = split[0] + " Has invited you for a game. Accept?";
            string caption = "Game invitation";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(this, message, caption, buttons,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (result == DialogResult.Yes)
            {
                switch(split[1])
                {
                    case "TicTacToe": _game = new TicTacToe(this, NameTb.Text); break;
                    default: _game = new TicTacToe(this, NameTb.Text); break; 
                }
              
             
                _game.startGame(split[0]);
                _presenter.send("Answer^" + NameTb.Text + "^Accept");

            }

        }      
        public void LoggedView()
        {
            StatusLb.Text = "Connected";
            LoginBtn.Text = "Disconnect";
            CancelBtn.Text = "Exit";

        }
        private void UnloggedView()
        {
            StatusLb.Text = "Disconnected";
            LoginBtn.Text = "Login";
            CancelBtn.Text = "Cancel";
            this.Height = 313;
        }
        private void ModificationPos_CheckedChanged(object sender, EventArgs e)
        {
            if (ModificationPos.Checked)
            {
                PortTb.Enabled = true;
                AddressTb.Enabled = true;
            }
        }
        private void CancelBtn_Click(object sender, EventArgs e)
        {
            if (StatusLb.Text =="Connected")
            {
            
                _presenter.send("disconnect");
            }
            Close();
        }
        private void LoginBtn_Click(object sender, EventArgs e)
        {

            if (ModificationPos.Checked)
            {
                _presenter = new Presenter(Convert.ToInt32(PortTb.Text), AddressTb.Text);
            };
            if (LoginBtn.Text == "Disconnect")
            {
                _presenter.send("disconnect");
                UnloggedView();
            }
            else
            {
                _presenter.send("Login^" + NameTb.Text);
            }


        }
        private void ModificationPos_MouseClick(object sender, MouseEventArgs e)
        {
            if (ModificationPos.Enabled)
            {
                ModificationPos.Checked = false;
            }
            else
            {
                ModificationPos.Checked = true;
            }
        }

        private void UserListLv_SelectedIndexChanged(object sender, EventArgs e)
        {
            OpNickName = UserListLv.SelectedItems[0].Text;
            switch (GameList.SelectedItems[0].Text)
            {
                case "TicTacToe":
                    _game = new TicTacToe(this, NameTb.Text); break;
                default: _game = new TicTacToe(this, NameTb.Text); break;

            }
            if (!SelectFlag)
            { 
                _presenter.send("Invite^" + NameTb.Text + "^target^" + UserListLv.SelectedItems[0].Text + "^" + GameList.Items[0].Text);
            }
            else
            {
            
                _presenter.send("Invite^" + NameTb.Text + "^target^" + UserListLv.SelectedItems[0].Text + "^" + GameList.SelectedItems[0].Text);
            }

        }
        private void UserListUpdate(string list)
        {
            string[] split = list.Split('^');
            UserListLv.Items.Clear();
            foreach (string p in split)
            {
                UserListLv.Items.Add(p);
            }
        }

        private void GameList_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            SelectFlag = true;
        }
       


    }

}

